const express = require("express")
const route = express.Router()

//引入权限控制校验中间件
const isLoginMiddel = require("../middleware/isLoginMiddel")

//引入数据库集合
const advModel = require("../db/model/addAdvModel")
route.get("/advList/:pageNo/:everyNum", isLoginMiddel, async (req, res) => {
    const {
        pageNo,
        everyNum
    } = req.params

    try {
        const findResult = await advModel.find({}).limit(everyNum).skip((pageNo - 1) * 3);
        //请求总数量
        const allAdvCount = await advModel.count({})
        res.send({
            code: 10000,
            msg: "ok",
            data:{
                findResult,
                allPageCount: Math.ceil(allAdvCount / everyNum)
            }
        })
    } catch (e) {
        res.send({
            code: 10001,
            msg: "服务器查询广告列表失败" + e.message
        })
    }
})

module.exports = route